package com.example.figurehecheng.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.figurehecheng.common.constant.MessageConstant;
import com.example.figurehecheng.common.context.BaseContext;
import com.example.figurehecheng.common.exception.AccountExist;
import com.example.figurehecheng.domain.dto.CanteenDTO;
import com.example.figurehecheng.domain.dto.TourtableDTO;
import com.example.figurehecheng.domain.entity.Canteen;
import com.example.figurehecheng.domain.entity.Merchant;
import com.example.figurehecheng.domain.entity.Tourtable;
import com.example.figurehecheng.domain.vo.CanteenVO;
import com.example.figurehecheng.domain.vo.ImageVO;
import com.example.figurehecheng.domain.vo.TourtableVO;
import com.example.figurehecheng.mapper.CanteenMapper;
import com.example.figurehecheng.service.ICanteenService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.figurehecheng.service.IMerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 宇科工作室
 * @since 2024-10-18
 */
@Service
public class CanteenServiceImpl extends ServiceImpl<CanteenMapper, Canteen> implements ICanteenService {
@Autowired
    private IMerchantService iMerchantService;

    /**
     * 添加+修改
     * @param canteenDTO
     */
    @Override
    public void updatetour(CanteenDTO canteenDTO) {

            if (!ObjectUtil.isAllNotEmpty(canteenDTO.getCookenName(), canteenDTO.getIntroduce(), canteenDTO.getAddress())) {
                throw new AccountExist(MessageConstant.COUPON_NOT_NULL);
            }

            Canteen canteen = BeanUtil.copyProperties(canteenDTO, Canteen.class);
            System.out.println(canteen);
        Canteen canteens = lambdaQuery()
                    .eq(Canteen::getShopId, BaseContext.getCurrentId())
                    .one();

            if (!(canteens==null)) {
                boolean update = lambdaUpdate()
                        .set(Canteen::getCookenName, canteen.getCookenName())
                        .set(Canteen::getType, canteen.getType())
                        .set(Canteen::getIntroduce, canteen.getIntroduce())
                        .set(Canteen::getEndTime,canteen.getEndTime())
                        .set(Canteen::getBeginTime,canteen.getBeginTime())
                        .set(Canteen::getAddress,canteen.getAddress())
                        .set(Canteen::getPhone,canteen.getPhone())
                        .set(Canteen::getCulture,canteen.getCulture())
                        .eq(Canteen::getShopId, BaseContext.getCurrentId())
                        .update();
            }
//
            if (canteens==null) {
                canteen.setShopId(BaseContext.getCurrentId().intValue());
                Merchant merchant = iMerchantService.getOne(new QueryWrapper<Merchant>()
                        .eq("id", BaseContext.getCurrentId().intValue()));
                canteen.setType(merchant.getRole());
                save(canteen);
            }

    }


    /**
     * 查询
     * @return
     */
    @Override
    public CanteenVO selectone() {
        CanteenVO can = new CanteenVO();
        Long currentId = BaseContext.getCurrentId();
        List<CanteenVO> tourlist = new ArrayList<>();
        List<Canteen> tour = lambdaQuery().
                eq(Canteen::getShopId, currentId)
                .list();
        tour.forEach(item -> {
            try {
                tourlist.add(BeanUtil.copyProperties(item, CanteenVO.class));
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        if (tourlist.size() > 1) {
            System.out.println("发布项目过多");
        } else if (tourlist.size() == 1) {
            can = tourlist.get(0);
        }
        return can;
    }


}
